home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 11 / FM Towns Free Software Collection 11.iso / t_os / tool / artemis1 / src / mag.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-11  |  6.7 KB  |  343 lines

  1. /*
  2.     ARTemis (Graphic Editor for FM-TOWNS)
  3.     (c) MATSUUCHI Ryosuke 1992-1993
  4.  
  5.     mag.c
  6. */
  7.  
  8.  
  9. #define    USEMAG    0
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <process.h>
  14. #include <egb.h>
  15. #include <conio.h>
  16. #include <ryosuke.h>
  17. #include <usrlib.h>
  18. #include <memory.h>
  19. #include <snd.h>
  20. #include <msdos.cf>
  21.  
  22.  
  23. #if USEMAG
  24.  
  25.  
  26. static void _setplt(int plt, int g, int r, int b)
  27. {
  28.     char pal_[4+8*16];
  29.     DWORD(pal_ + 0) = 1;
  30.     DWORD(pal_ + 4) = plt;
  31.     BYTE (pal_ + 8) = b<<4;
  32.     BYTE (pal_ + 9) = r<<4;
  33.     BYTE (pal_ +10) = g<<4;
  34.     BYTE (pal_ +11) = 0;
  35.     EGB_palette(EGB_work,0,pal_);
  36. }
  37.  
  38.  
  39. #endif
  40.  
  41.  
  42. int MAG_easyLoad(char *fname)
  43. {
  44. #if USEMAG
  45.     FILE *fp_a, *fp_b, *fp_p;
  46.     int i,j,k;
  47.     if ((fp_a = fopen(fname, "rb")) == NULL)    // フラグA読出用
  48.         return -1;
  49.     if ((fp_b = fopen(fname, "rb")) == NULL)    // フラグB読出用
  50.         return -1;
  51.     if ((fp_p = fopen(fname, "rb")) == NULL)    // ピクセルデータ読出用
  52.         return -1;
  53.  
  54.     // チェック/コメント領域をよみとばす
  55.     for (i=0; i<32; i++)
  56.         fgetc(fp_a);
  57.     int c;
  58.     do {
  59.         c = fgetc(fp_a);
  60.     } while (c != 0x1a);
  61.  
  62.     // ヘッダ領域のファイル内オフセットを記録する
  63.     int ofs_head;
  64.     ofs_head = ftell(fp_a);
  65.  
  66.     // ヘッダを読み込んで調べる
  67.     char head[32];
  68.     fread(head, 1,32, fp_a);
  69.     int ofs_a, ofs_b, ofs_pix, siz_b, siz_pix;
  70.     ofs_a = DWORD(head+12);
  71.     ofs_b = DWORD(head+16);
  72.     siz_b = DWORD(head+20);
  73.     ofs_pix = DWORD(head+24);
  74.     siz_pix = DWORD(head+28);
  75.     
  76.     // パレットを読み込んで設定する
  77.     for (i=0; i<16; i++) {
  78.         int g,r,b;
  79.         g = fgetc(fp_a) >> 4;
  80.         r = fgetc(fp_a) >> 4;
  81.         b = fgetc(fp_a) >> 4;
  82.         _setplt(i, g,r,b);
  83.     }
  84.     
  85.     // フラグA/B, ピクセルデータ用のファイル読出位置を設定する
  86.     fseek(fp_a, ofs_head + ofs_a, SEEK_SET);
  87.     fseek(fp_b, ofs_head + ofs_b, SEEK_SET);
  88.     fseek(fp_p, ofs_head + ofs_pix, SEEK_SET);
  89.  
  90.     // 1ラインずつ展開していく
  91.     char fbbuf1[80], fbbuf2[80];
  92.     char linebuf[17][640];
  93.     for (i=0; i<400; i++) {
  94.         char *lbp;
  95.         lbp = linebuf[i % 17];
  96.  
  97.         // 1ライン分のフラグB(80 bytes)を展開する
  98.         for (j=0; j<((640/4)/2)/8; j++) {
  99.             int a;  static char mask[] = {128,64,32,16,8,4,2,1};
  100.             a = fgetc(fp_a);
  101.             for (k=0; k<8; k++) {
  102.                 if ((a & mask[k]) != 0)
  103.                     fbbuf1[j*8+k] = fgetc(fp_b);
  104.                 else
  105.                     fbbuf1[j*8+k] = 0;
  106.             }
  107.         }
  108.         if (i > 0) {
  109.             for (j=0; j<80; j++)
  110.                 fbbuf1[j] ^= fbbuf2[j];
  111.         }
  112.         memcpy(fbbuf2, fbbuf1, 80);
  113.         // 1ラインぶんのピクセルを展開する
  114.         static int ofsx[] = {0,-1,-2,-4,0,-1,0,-1,-2,0,-1,-2,0,-1,-2,0};
  115.         static int ofsy[] = {0,0,0,0,-1,-1,-2,-2,-2,-4,-4,-4,-8,-8,-8,-16};
  116.         for (j=0; j<160; j++) {
  117.             int b,rx,ry,x,y;
  118.             b = ((j&1)==0 ? (fbbuf1[j/2]>>4) : (fbbuf1[j/2]&15));
  119.             x = j*4;
  120.             y = i;
  121.             if (b == 0) {
  122.                 int c;
  123.                 c = fgetc(fp_p);
  124.                 lbp[j*2] = (c>>4) + (c&15)*16;
  125.                 c = fgetc(fp_p);
  126.                 lbp[j*2+1] = (c>>4) + (c&15)*16;
  127.             } else {
  128.                 rx = j*4 + ofsx[b]*4;
  129.                 ry = i   + ofsy[b];
  130.                 *(short int *)&lbp[j*2] = *(short int *)&linebuf[ry%17][rx/2];
  131.             }
  132.         }
  133.  
  134.         {
  135.             para_putBlock para;
  136.             para.buf = lbp;
  137.             para.bufsel = getds();
  138.             para.x1 = 0;
  139.             para.x2 = 639;
  140.             para.y1 = para.y2 = i;
  141.             EGB_putBlock(EGB_work, 0, (char*)¶);
  142.         }
  143.     }
  144.     fclose(fp_p);
  145.     fclose(fp_b);
  146.     fclose(fp_a);
  147. #else
  148.     return 0;
  149. #endif
  150. }
  151.  
  152.  
  153. #if USEMAG
  154.  
  155. #define    fAlen    256
  156.  
  157. static    FILE    *fA_fp;
  158. static    int        fA_ofs;
  159. static    char    fAbuf[fAlen];
  160. static    int        fAbyteptr, fAbitptr;
  161.  
  162. static void fAinit(FILE *fp, int ofs)
  163. {
  164.     fA_fp = fp;
  165.     fA_ofs = ofs;
  166.     fAbyteptr = fAbitptr = 0;
  167.     fAbuf[0] = 0;
  168. }
  169.  
  170. static void fAout(int bit)
  171. {
  172.     static char _bit[] = {128,64,32,16,8,4,2,1};
  173.     if (bit)
  174.         fAbuf[fAbyteptr] |= _bit[fAbitptr];
  175.     fAbitptr++;
  176.     if (fAbitptr >= 8) {
  177.         fAbitptr = 0;
  178.         fAbyteptr++;
  179.         if (fAbyteptr >= fAlen) {
  180.             int curofs;
  181.             curofs = ftell(fA_fp);
  182.             fseek(fA_fp, fA_ofs, SEEK_SET);
  183.             fwrite(fAbuf, 1, fAlen, fA_fp);
  184.             fseek(fA_fp, curofs, SEEK_SET);
  185.             fA_ofs += fAlen;
  186.             fAbyteptr = 0;
  187.         }
  188.         fAbuf[fAbyteptr] = 0;
  189.     }
  190. }
  191.  
  192. static void fAbyteflush()
  193. {
  194.     if (fAbitptr != 0) {
  195.         fAbyteptr++;
  196.         fAbitptr = 0;
  197.         if (fAbyteptr >= fAlen) {
  198.             int curofs;
  199.             curofs = ftell(fA_fp);
  200.             fseek(fA_fp, fA_ofs, SEEK_SET);
  201.             fwrite(fAbuf, 1, fAlen, fA_fp);
  202.             fseek(fA_fp, curofs, SEEK_SET);
  203.             fA_ofs += fAlen;
  204.             fAbyteptr = 0;
  205.         }
  206.         fAbuf[fAbyteptr] = 0;
  207.     }
  208. }
  209.  
  210. static void fAflush()
  211. {
  212.     int len;
  213.     len = fAbyteptr + (fAbitptr+7)/8;
  214.     if (len > 0) {
  215.         int curofs;
  216.         curofs = ftell(fA_fp);
  217.         fseek(fA_fp, fA_ofs, SEEK_SET);
  218.         fwrite(fAbuf, 1, len, fA_fp);
  219.         fseek(fA_fp, curofs, SEEK_SET);
  220.         fA_ofs += len;
  221.     }
  222.     fAbyteptr = fAbitptr = 0;
  223.     fAbuf[fAbyteptr] = 0;
  224. }
  225.  
  226. #endif
  227.  
  228.  
  229. #if 0
  230.  
  231. int MAG_save(char *fname, int x1, int y1, int xlen, int ylen, char *memo)
  232. // memo : MAGファイルに書き込むメモ.
  233. {
  234.     FILE *fp;
  235.     if ((fp = fopen(fname, "wb")) == NULL) {
  236.         return -1;
  237.     }
  238.     // チェックデータ/コメントを書き込む
  239.     {
  240.         char chkdata[32];
  241.         memcpy(chkdata, "MAKI02  ", 8);
  242.         memcpy(chkdata+8, "TOWNS", 4);
  243.         memcpy(chkdata+12, ">謎<              ", 18);
  244.         memset(chkdata+30,0,2);
  245.         fwrite(chkdata, 1,32, fp);
  246.         {
  247.             _setmode(fp, _TEXT);
  248.             char *p;
  249.             for (p=memo; *p!=0; p++)
  250.                 fputc(*p, fp);
  251.             _setmode(fp, _BINARY);
  252.         }
  253.         fputc(0x1a, fp);
  254.     }
  255.     // ヘッダ先頭のファイル内オフセットを記録する
  256.     int ofs_head;
  257.     ofs_head = ftell(fp);
  258.     // ダミーヘッダを作成する
  259.     {
  260.         char dmy[32];
  261.         fwrite(dmy, 1,32, fp);
  262.     }
  263.     // ダミーパレットを作成する
  264.     {
  265.         char dmy[48];
  266.         fwrite(dmy, 1, 48, fp);
  267.     }
  268.     // セーブ範囲のX座標を8単位に補正し、フラグAのサイズを計算する
  269.     int siz_a,ofs_a,ofs_b;
  270.     {
  271.         int x2;
  272.         x2 = x1 + xlen -1;
  273.         x1 = x1 - x1 % 8;
  274.         x2 = x2 - x2 % 8 + 7;
  275.         xlen = x2-x1+1;
  276.         siz_a = ((xlen/4+7)/8)*ylen;    // アブナーイ!
  277.             // フラグAが、1ラインについてバイト境界におさまらなかったら、
  278.             // どうすればいいんだろう...
  279.         if (siz_a & 1)
  280.             siz_a++;
  281.     }
  282.     ofs_a = ftell(fp);
  283.     ofs_b = ofs_a + siz_a;
  284.     // ダミーのフラグAを出力
  285.     {
  286.         int i;
  287.         for (i=0; i<siz_a; i++)
  288.             fputc(0, fp);
  289.     }
  290.     // フラグA, Bを出力していく
  291.     {
  292.         static int ofsx[] = {0,-1,-2,-4,0,-1,0,-1,-2,0,-1,-2,0,-1,-2,0};
  293.         static int ofsy[] = {0,0,0,0,-1,-1,-2,-2,-2,-4,-4,-4,-8,-8,-8,-16};
  294.         int y,x;
  295.         char linebuf[17][xlen];
  296.         for (y=0; y < ylen; y++) {
  297.             char *lbp;
  298.             lbp = linebuf[y % 17];
  299.             grgetblk(lbp, x1,y1+y, xlen,1);
  300.             for (x=0; x<xlen; x+=8) {
  301.                 int pixel;
  302.                 // 8ドットのうち、はじめの4ドット(1ピクセル)を処理
  303.                 pixel = *(short int *)(lbp + x/2);
  304.                 int b;
  305.                 bool found;
  306.                 found = NO;
  307.                 for (b=1; b<=15; b++) {
  308.                     int sx,sy;
  309.                     sx = x+ofsx[b]*4;
  310.                     sy = y+ofsy[b];
  311.                     if (sx<0)
  312.                         continue;
  313.                     if (sy<0)
  314.                         break;
  315.                     if (pixel == *(short int *)(linebuf[sy % 17] + sx/2))
  316.                         found = YES;
  317.                         
  318.                     
  319.                     
  320.                     
  321.                     
  322.  
  323.                 }
  324.  
  325.                 // 8ドットのうち、2つめの4ドット(1ピクセル)を処理
  326.  
  327.                 // フラグBを出力
  328.             }
  329.         }
  330.     }
  331.     
  332.     
  333.     
  334.     
  335.     
  336.     
  337.     
  338. }
  339.  
  340. #endif
  341.  
  342. // end of mag.c
  343.